package cc.zstart.generator.mybatis.mapper;

import java.util.Set;
import java.util.TreeSet;

import cc.zstart.generator.mybatis.plugin.AmanPluginConfig;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
import org.mybatis.generator.codegen.mybatis3.javamapper.JavaMapperGenerator;

/**
 * @desc <p></p>
 * @author DaiJiangGuo
 * @date 2015年5月12日下午3:40:34
 */
public class ExtendsJavaMapperGenerator extends JavaMapperGenerator{
    private AmanPluginConfig apc;

    public ExtendsJavaMapperGenerator(AmanPluginConfig apc) {
        super();
        this.apc = apc;
        this.setContext(apc.context);
        this.setIntrospectedTable(apc.introspectedTable);
        initialized();
    }


    public void initialized() {
    }

    /**
     * @author DaiJiangGuo
     * @data 2015年5月12日 上午11:15:11
     * @param interfaze
     * @param introspectedTable
     */
    public void addQueryByPagerMethod(Interface interfaze, IntrospectedTable introspectedTable, String name) {
        if(apc.serviceMethod_gen_queryByPager){
            Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
            Method method = new Method();

            method.setVisibility(JavaVisibility.PUBLIC);
            FullyQualifiedJavaType returnType = new FullyQualifiedJavaType("java.util.List<"+introspectedTable.getBaseRecordType()+">");
            method.setReturnType(returnType);
            importedTypes.add(returnType);
            method.setName(name);

            importedTypes.add(apc.pagerType);
            method.addParameter(new Parameter(apc.pagerType, "pager")); //$NON-NLS-1$

            context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);

            interfaze.addImportedTypes(importedTypes);
            interfaze.addMethod(method);
        }
    }

    /**
     * @author DaiJiangGuo
     * @data 2015年5月12日 上午11:15:08
     * @param interfaze
     * @param introspectedTable
     */
    public void addQueryByEntityMethod(Interface interfaze, IntrospectedTable introspectedTable, String name) {
        Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
        Method method = new Method();

        method.setVisibility(JavaVisibility.PUBLIC);
        FullyQualifiedJavaType returnType = new FullyQualifiedJavaType("java.util.List<"+introspectedTable.getBaseRecordType()+">");
        method.setReturnType(returnType);
        importedTypes.add(returnType);
        method.setName(name);

        FullyQualifiedJavaType parameterType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());

        importedTypes.add(parameterType);
        method.addParameter(new Parameter(parameterType, "entity")); //$NON-NLS-1$

        context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);

        interfaze.addImportedTypes(importedTypes);
        interfaze.addMethod(method);

    }

    /**
     * @author DaiJiangGuo
     * @data 2015年5月12日 上午11:15:04
     * @param interfaze
     * @param introspectedTable
     */
    public void addQueryByMapMethod(Interface interfaze, IntrospectedTable introspectedTable, String name) {
        Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
        Method method = new Method();

        method.setVisibility(JavaVisibility.PUBLIC);
        FullyQualifiedJavaType returnType = new FullyQualifiedJavaType("java.util.List<"+introspectedTable.getBaseRecordType()+">");
        method.setReturnType(returnType);
        importedTypes.add(returnType);
        method.setName(name);

        FullyQualifiedJavaType parameterType = new FullyQualifiedJavaType("java.util.Map<String, Object>");

        importedTypes.add(parameterType);
        method.addParameter(new Parameter(parameterType, "paramMap")); //$NON-NLS-1$

        context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);

        interfaze.addImportedTypes(importedTypes);
        interfaze.addMethod(method);

    }



}
